﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;
using System.Runtime.Serialization.Json;
using System.Runtime.Serialization;
using System.Configuration;
using System.Web;
namespace AliPayWinForm
{
    public partial class Alipay : Form
    {
        public Alipay()
        {
            InitializeComponent();
            BindData();
        }
        /// <summary>
        /// 初始化数据
        /// </summary>
        private void BindData()
        {

            List<AlipayModel> alipayList = new List<AlipayModel>();
            string ACCOUNT_URL = ConfigurationManager.AppSettings["Account_URL"];
            string jsonStr = HttpHelper.GetHttpResponse(ACCOUNT_URL);
            DataContractJsonSerializer s = new DataContractJsonSerializer(alipayList.GetType());
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonStr));
            alipayList = (List<AlipayModel>)s.ReadObject(ms);
            ms.Dispose();
            combbox.DataSource = alipayList;
            combbox.ValueMember = "id";
            combbox.DisplayMember = "account_num";
        }

        /// <summary>
        /// 设置账号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {

            if (this.webBrowser1.Document.GetElementById("J-input-user") == null)
                return;
            this.webBrowser1.Document.GetElementById("J-input-user").SetAttribute("value", combbox.Text.ToString());
            this.webBrowser1.Document.GetElementById("password_rsainput").SetAttribute("value", txt_pwd.Text);

        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            if (this.webBrowser1.Document.GetElementById("J-login-btn") == null)
                return;

            this.webBrowser1.Document.GetElementById("J-login-btn").InvokeMember("click");
        }

        List<AlipayCookie> alipayCookie = new List<AlipayCookie>();
        /// <summary>
        /// 获取 cookie
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            string account = combbox.Text.ToString();
            string cookie = this.webBrowser1.Document.Cookie;
            if (alipayCookie.Count == 0)
            {
                AlipayCookie alipay = new AlipayCookie(account, cookie);
                alipayCookie.Add(alipay);
            }
            else
            {
                foreach(AlipayCookie alipays in alipayCookie){
                    if (alipays.account == account)
                    {
                        alipayCookie.Remove(alipays);
                        break;
                    }
                 }
                AlipayCookie alipay = new AlipayCookie(account, cookie);
                alipayCookie.Add(alipay);
            }
          
          
           // MessageBox.Show(this.webBrowser1.Document.Cookie);
        }


        /// <summary>
        /// 下拉框选择事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void combbox_SelectedIndexChanged(object sender, EventArgs e)
        {
            AlipayModel model = (sender as ComboBox).SelectedItem as AlipayModel;
            txt_pwd.Text = model.account_pwd.ToString();
        }

        /// <summary>
        /// 同步账号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_tb_Click(object sender, EventArgs e)
        {
            BindData();
        }
        /// <summary>
        /// 发送cookie
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Send_Cookie_Click(object sender, EventArgs e)
        {
            SendCookie();
        }
        /// <summary>
        /// 发送post json cookie
        /// </summary>
        private void SendCookie()
        {
            string POST_JSON_STR = HttpHelper.ObjectToJSON<List<AlipayCookie>>(alipayCookie);
            string COOKIES_URL = ConfigurationManager.AppSettings["COOKIES_URL"];
            HttpHelper.CreatePostHttpResponse(COOKIES_URL, POST_JSON_STR);

        }


    }
    /// <summary>
    /// 支付宝账号信息
    /// </summary>
    [DataContract]
    public class AlipayModel
    {
        public AlipayModel(string id, string account_num, string account_pwd)
        {
            this.id = id;
            this.account_num = account_num;
            this.account_pwd = account_pwd;
        }
        [DataMember]
        public string id { get; set; }
        [DataMember]
        public string account_num { get; set; }
        [DataMember]
        public string account_pwd { get; set; }
    }
    [DataContract]
    public class AlipayCookie
    {

        public AlipayCookie(string account, string cookies)
        {
            this.account = account;
            this.cookies = cookies;
        }
        [DataMember]
        public string account { get; set; }
        [DataMember]
        public string cookies { get; set; }
    }
}
